// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccessTypeResolver.cs" company="">
//   
// </copyright>
// <summary>
//   The access type resolver.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace XRay.Library
{
    using System;

    using AutoMapper;

    using Microsoft.WindowsAzure.Storage.Table;

    using XRay.Library.Contracts;

    /// <summary>
    /// The access type resolver.
    /// </summary>
    public class AccessTypeResolver : ValueResolver<TableEntity, AccessTypeEnum>
    {
        #region Methods

        /// <summary>
        /// The resolve core.
        /// </summary>
        /// <param name="source">
        /// The source.
        /// </param>
        /// <returns>
        /// The <see cref="AccessTypeEnum"/>.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        protected override AccessTypeEnum ResolveCore(TableEntity source)
        {
            var split = source.RowKey.Split(';');

            if (split.Length != 2)
            {
                throw new ArgumentOutOfRangeException("entity.rowkey");
            }

            return (AccessTypeEnum)Enum.Parse(typeof(AccessTypeEnum), split[0]);
        }

        #endregion
    }
}